using System;
using Microsoft.SPOT;

namespace Enough.Connectivity
{
    public static class BinaryConnectionExtensions
    {
        public static void SendString(this IBinaryConnection connection, string data, bool sendLength = false)
        {
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            if (sendLength)
            {
                int length = buffer.Length;
                SendInt(connection, length);
            }
            connection.SendData(buffer);
        }

        public static void SendInt(this IBinaryConnection connection, int value)
        {
            byte[] intData = new byte[4];
            intData[0] = (byte)(value >> 24);
            intData[1] = (byte)((value >> 16) & 0x00ff);
            intData[2] = (byte)((value >> 8) & 0x0000ff);
            intData[3] = (byte)((value & 0x000000ff));
            connection.SendData(intData);
        }        
    }
}
